package weaver.tools.util;

import cn.hutool.core.date.DateUnit;
import weaver.conn.RecordSet;
import weaver.general.Util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Title: Ecology9
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2024-04-15
 * @Description: 日期工具类
 * @ModifyLog:
 **/

public class DateUtil {

    /**
     *@Description: 分钟差
     */
    public static long getDiffMinute(String begintime, String endtime) {
        Date data1 = cn.hutool.core.date.DateUtil.parse(begintime);
        Date date2 = cn.hutool.core.date.DateUtil.parse(endtime);
        return cn.hutool.core.date.DateUtil.between(data1, date2, DateUnit.SECOND);
    }

    /**
     *@Description: 根据时间 和时间格式 校验是否正确
     */
    public static boolean isLegalDate(int length, String sDate, String format) {
        if ((sDate == null) || (sDate.length() != length)) {
            return false;
        }
        DateFormat formatter = new SimpleDateFormat(format);
        try {
            Date date = formatter.parse(sDate);
            return sDate.equals(formatter.format(date));
        } catch (Exception e) {
            return false;
        }
    }

    /**
     *@Description: 秒转时分秒
     */
    public static String secondConvertHourMinSecond(Long second) {
        String str = "00时00分00秒";
        if (second == null || second < 0) {
            return str;
        }

        // 得到小时
        long h = second / 3600;
        str = h > 0 ? ((h < 10 ? ("0" + h) : h) + ":") : "00h";

        // 得到分钟
        long m = (second % 3600) / 60;
        str += (m < 10 ? ("0" + m) : m) + "m";

        //得到剩余秒
        long s = second % 60;
        str += (s < 10 ? ("0" + s) : s) + "s";
        return str;
    }

    /**
     *@Description:  比较年月
     */
    public static int compareToYearMonth(String yearMonth1, String yearMonth2) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Date date1 = sdf.parse(yearMonth1);
            Date date2 = sdf.parse(yearMonth2);
            int result = date1.compareTo(date2);
            if (result < 0) {
                System.out.println("第一个年月早于第二个年月");
            } else if (result > 0) {
                System.out.println("第一个年月晚于第二个年月");
            } else {
                System.out.println("两个年月相等");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     *@Description: 判断当前日期是否属于参数日期范围
     */
    public static boolean isWithinRange(String begDate,String endDate){
        String currentDate = weaver.common.DateUtil.getCurrentDate();
        Date curdate = weaver.common.DateUtil.parseToDate(currentDate);
        Date begdate = weaver.common.DateUtil.parseToDate(begDate);
        Date enddate = weaver.common.DateUtil.parseToDate(endDate);
        return !(curdate.before( begdate )|| curdate.after( enddate ));
    }

    /**
     *@Description: 日期范围拆分日期为每一天
     */
    public static List<LocalDate> splitByDateBetween(String begindate, String enddate) {
        //开始日期
        LocalDate beginDate = LocalDate.parse(begindate);
        //结束日期
        LocalDate endDate = LocalDate.parse(enddate);
        LocalDate date = beginDate;
        List<LocalDate> dates = new ArrayList<>();
        //遍历日期
        while (!date.equals(endDate.plusDays(1))) {
            dates.add(date);
            date = date.plusDays(1);
        }
        return dates;
    }

    /**
     *@Description:  判断是否是工作日
     */
    public boolean isWorkDay(String currentDate) {
        boolean flag = true;
        RecordSet rs = new RecordSet();
        int week = weaver.common.DateUtil.getWeek(currentDate);
        rs.executeQuery("select count(id) from kq_HolidaySet where holidayDate=?", currentDate);
        rs.next();
        int count = Util.getIntValue(rs.getString(1));
        if (count > 0 || week > 5) {
            flag = false;
        }
        return flag;
    }

    /**
     *@Description: 日期格式化返回yyyyMMdd
     */
    public static String formatDateToSAP(String datestr) {
        String formatTime = "";
        if (!"".equals(datestr) && Util.getIntValue(datestr, -1) < 0) {
            try {
                SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
                Date date1 = date.parse(datestr);
                formatTime = new SimpleDateFormat("yyyyMMdd").format(date1);
            } catch (ParseException e) {
                formatTime = datestr;
            }
        }
        return formatTime;
    }

    /**
     *@Description: 日期格式化返回yyyy-MM-dd
     */
    public static String formatDateToOA(String datestr) {
        String formatTime = "";
        if (!"".equals(datestr) && Util.getIntValue(datestr, -1) > 0) {
            try {
                SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd");
                Date date1 = date.parse(datestr);
                formatTime = new SimpleDateFormat("yyyy-MM-dd").format(date1);
            } catch (ParseException e) {
                formatTime = datestr;
            }
        }
        return formatTime;
    }

    /*
     *@Description: 日期格式转换(-转换为年月日)
     */
    public static String converDateChinese(String date) {
        String newDate = "";
        if (date.contains( "年" )) {
            String b1 = date.replace( "年", "_" );
            String b2 = b1.replace( "月", "_" );
            newDate = b2.replace( "日", " ").trim();
        } else {
            newDate = date;
        }
        return newDate;
    }

    /*
     *@Description: 参数日期是否大于当前日期
     */
    public static boolean IsGrethanCurDate(String date) {
        boolean flag = false;
        long ts1 = 0L;
        long ts2 = 0L;
        try {
            if(date.length()>10){
                SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
                Date d1 = sdf.parse( date );
                ts1 = d1.getTime();
                Date d2 = sdf.parse( weaver.common.DateUtil.getFullDate() );
                ts2 = d2.getTime();
            }else {
                SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
                Date d1 = sdf.parse( date );
                ts1 = d1.getTime();
                Date d2 = sdf.parse( weaver.common.DateUtil.getCurrentDate() );
                ts2 = d2.getTime();
            }
            if((ts1 - ts2)>=0){
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /*
     *@Description: 将时间戳转换为时间
     */
    public static String stampToDate(Long systemdate){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(systemdate);
        String res = simpleDateFormat.format(date);
        return res;
    }
    /*
     *@Description: 将时间转换为时间戳
     */
    public static String dateToStamp(String s){
        String res = "";
        try{
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = simpleDateFormat.parse(s);
            long ts = date.getTime();
            res = String.valueOf(ts);
        }catch (ParseException e){
            e.getErrorOffset();
        }
        return res;
    }

    /**
     *@Description: 参数日期减去当前日期获取天数
     */
    public static long getDays(String date) {
        long days = 0L;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
            Date d1 = sdf.parse( date );
            long ts1 = d1.getTime();
            Date d2 = sdf.parse( weaver.common.DateUtil.getCurrentDate() );
            long ts2 = d2.getTime();
            days = (ts1 - ts2) / (1000 * 60 * 60 * 24);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return days;
    }
}
